<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>发送广播消息</span>
        </div>
      </template>
      
      <el-form
        ref="broadcastForm"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="broadcast-form"
      >
        <el-form-item label="消息标题" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入消息标题"
            maxlength="50"
            show-word-limit
          />
        </el-form-item>
        
        <el-form-item label="消息内容" prop="context">
          <el-input
            v-model="form.context"
            type="textarea"
            :rows="6"
            placeholder="请输入消息内容"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">发送广播</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'Broadcast',
  data() {
    return {
      form: {
        title: '',
        context: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入消息标题', trigger: 'blur' },
          { min: 2, max: 50, message: '标题长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        context: [
          { required: true, message: '请输入消息内容', trigger: 'blur' },
          { min: 5, max: 500, message: '内容长度在 5 到 500 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 初始化消息历史记录
    this.initMessageHistory()
  },
  methods: {
    initMessageHistory() {
      // 从 localStorage 获取消息历史
      const messageHistory = localStorage.getItem('messageHistory')
      if (!messageHistory) {
        localStorage.setItem('messageHistory', JSON.stringify([]))
      }
    },
    addMessageToHistory(message) {
      const history = JSON.parse(localStorage.getItem('messageHistory') || '[]')
      history.push({
        ...message,
        timestamp: new Date().getTime(),
        read: false
      })
      localStorage.setItem('messageHistory', JSON.stringify(history))
    },
    markMessageAsRead(messageId) {
      const history = JSON.parse(localStorage.getItem('messageHistory') || '[]')
      const updatedHistory = history.map(msg => {
        if (msg.timestamp === messageId) {
          return { ...msg, read: true }
        }
        return msg
      })
      localStorage.setItem('messageHistory', JSON.stringify(updatedHistory))
    },
    submitForm() {
      this.$refs.broadcastForm.validate(valid => {
        if (valid) {
          request({
            url: '/message/onlineBroadCasting',
            method: 'post',
            headers: {
              'Content-Type': 'application/json'
            },
            data: {
              title: this.form.title,
              context: this.form.context
            }
          }).then(res => {
            if (res.code === 200) {
              // 将消息添加到历史记录
              this.addMessageToHistory({
                title: this.form.title,
                context: this.form.context
              })
              this.$modal.msgSuccess('广播消息发送成功')
              this.resetForm()
            } else {
              this.$modal.msgError('发送失败：' + (res.msg || '未知错误'))
            }
          }).catch(error => {
            this.$modal.msgError('发送失败：' + (error.message || '未知错误'))
          })
        }
      })
    },
    resetForm() {
      this.$refs.broadcastForm.resetFields()
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.box-card {
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
}

.broadcast-form {
  margin-top: 20px;
}

.el-form-item__label {
  font-weight: 500;
}

.el-input__wrapper {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

.el-textarea__inner {
  font-family: inherit;
}
</style> 